package li.nibble.twittertido.adapter;

import java.util.ArrayList;

import li.nibble.twittertido.R;
import li.nibble.twittertido.TweetView;
import li.nibble.twittertido.beans.Tweet;
import li.nibble.twittertido.lazyimage.ImageLoader;
import li.nibble.twittertido.webservice.Client;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TweetAdapter extends ArrayAdapter<Tweet> {

	private ArrayList<Tweet> items;

	LayoutInflater vi;
	public ImageLoader imageLoader;

	public TweetAdapter(Context context, int textViewResourceId,
			ArrayList<Tweet> items) {
		super(context, textViewResourceId, items);
		this.items = items;
		vi = (LayoutInflater) getContext().getSystemService(
				Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(context);
	}

	public static class ViewHolder {
		public TextView tweet;
		public TextView user;
		public ImageView image;
		public LinearLayout options;
		public ImageView like;
		public ImageView dislike;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;

		final ViewHolder holder;

		if (v == null) {
			v = vi.inflate(R.layout.tweet_row, null);
			holder = new ViewHolder();
			holder.image = (ImageView) v.findViewById(R.id.avatar_user);
			holder.tweet = (TextView) v.findViewById(R.id.tweet);
			holder.user = (TextView) v.findViewById(R.id.user);
			holder.options = (LinearLayout) v.findViewById(R.id.tweet_options);
			holder.like = (ImageView) v.findViewById(R.id.option_like);
			holder.dislike = (ImageView) v.findViewById(R.id.option_dislike);
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}

		final Tweet tweet = items.get(position);

		holder.tweet.setText(tweet.getTweet());
		holder.user.setText("@" + tweet.getUser());
		imageLoader.DisplayImage(
				"https://api.twitter.com/1/users/profile_image?screen_name="
						+ tweet.getUser() + "&size=normal", holder.image);

		if (tweet.isOptionsOpen()) {
			holder.options.setVisibility(View.VISIBLE);
		} else {
			holder.options.setVisibility(View.GONE);
		}

		// Log.e("TweetAdapter.like", tweet.getId() + ". " + tweet.getLikes());

		if (tweet.getLikes() > 0) {
			holder.like.setImageResource(R.drawable.ic_like_green);
			holder.dislike.setImageResource(R.drawable.ic_dislike);
		} else if (tweet.getLikes() < 0) {
			holder.dislike.setImageResource(R.drawable.ic_dislike_red);
			holder.like.setImageResource(R.drawable.ic_like);
		} else {
			holder.like.setImageResource(R.drawable.ic_like);
			holder.dislike.setImageResource(R.drawable.ic_dislike);
		}

		v.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				openCloseOptions(
						(LinearLayout) v.findViewById(R.id.tweet_options),
						tweet);
			}
		});

		holder.like.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				like(tweet, holder.like);
			}
		});
		holder.dislike.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dislike(tweet, holder.dislike);
			}
		});

		v.findViewById(R.id.option_reply).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						reply(tweet.getId());
					}
				});
		v.findViewById(R.id.option_share).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						share(tweet);
					}
				});

		return v;
	}

	public void like(Tweet tweet, View v) {
		if (tweet.getLikes() == 0) {
			Client.like(getContext(), tweet.getId(), Tweet.LIKE);
			tweet.setLikes(Tweet.LIKE);
		}

		if (tweet.getLikes() == Tweet.LIKE) {
			((ImageView) v).setImageResource(R.drawable.ic_like_green);
		} else if (tweet.getLikes() == Tweet.DISLIKE) {
			((ImageView) v).setImageResource(R.drawable.ic_like);
		}
	}

	public void dislike(Tweet tweet, View v) {
		if (tweet.getLikes() == 0) {
			Client.like(getContext(), tweet.getId(), Tweet.DISLIKE);
			tweet.setLikes(Tweet.DISLIKE);
		}

		if (tweet.getLikes() == Tweet.DISLIKE) {
			((ImageView) v).setImageResource(R.drawable.ic_dislike_red);
		} else if (tweet.getLikes() == Tweet.DISLIKE) {
			((ImageView) v).setImageResource(R.drawable.ic_dislike);
		}
	}

	public void reply(int id) {
		Intent intent = new Intent(this.getContext(), TweetView.class);
		intent.putExtra("id_tweet", id);
		this.getContext().startActivity(intent);
	}

	public void share(Tweet tweet) {
		Intent sharingIntent = new Intent(Intent.ACTION_SEND);
		sharingIntent.setType("text/plain");
		sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
				getContext().getString(R.string.shared_via));

		String share_text = tweet.getTweet();
		
		
		
		
		if (share_text.length() > 114 - tweet.getUser().length()) {
			share_text = share_text.substring(0, share_text.length() -20 - tweet.getUser().length()-1-5);
		}

		share_text += "... @" + tweet.getUser();

		share_text += " http://twittertido.com.es/tweet.php?id="
				+ tweet.getId();

		sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, share_text);
		getContext().startActivity(
				Intent.createChooser(sharingIntent,
						getContext().getString(R.string.share_it)));
	}

	public void openCloseOptions(LinearLayout more_options, Tweet tweet) {
		if (more_options.getVisibility() == View.GONE) {
			more_options.setVisibility(View.VISIBLE);
			tweet.setOptionsOpen(true);
		} else {
			more_options.setVisibility(View.GONE);
			tweet.setOptionsOpen(false);
		}
	}

}
